//答题
module.exports.createRoom = function(roomId,channelService,GameRemote) {
	console.log("创建房间222:"+roomId)
	//继承gameBasic
	var gameBasic = require("./basic/gameBasic.js")
	var room = new gameBasic(roomId,"answer",1,channelService,GameRemote)
	var local = {}
	var tiku = require("./answerJson/testTiku1.json")
	var max_topic_count = 12 		//总题目数量
	var cur_topic_count = 0			//当前题目数量
	var cur_index = -1 				//当前题目下标
	var cur_use_topic = 0   		//当前使用过的题目
	var cur_answer = -1 			//当前题目答案
	var cur_topic = {}  			//当前题目内容
	var answerTime = 15000 			//答题时间
	var overTime = 300000    		//等待复活倒计时时间
	var countDown = 0 				//答题倒计时时间
	var curOverTime = 0 			//当前复活倒计时
	var topicTimer = 0 				//答题计时器
	var overTimer = 0 				//游戏结束倒计时
	var state = 0 					// 0 未开始   1 答题中   2  等待复活中
	var useTikuList = {}
	var lock = false 				//操作锁
	console.log("tiku")
	// console.log(tiku)
	//游戏开始
	local.gameBegin = function() {
		state = 1
		room.gameBegin()
		local.chooseTopic()
	}
	//开始选题
	local.chooseTopic = function() {
		var tmpIndex = -1
		cur_topic_count++
		cur_use_topic++
		if(cur_use_topic > tiku.length){
			//当前题目大于题库时随机选题
			tmpIndex = Math.floor(Math.random() * tiku.length)
			//确保不出现连续相同的题目
			if(cur_index == tmpIndex){
				tmpIndex = (tmpIndex + 1) % tiku.length
			}
		}else{
			//在题库中选择不重复的题目
			var rand = Math.floor(Math.random() * tiku.length)
			console.log("rand : "+rand)
			for(var i = 0;i < tiku.length;i++){
				tmpIndex = (rand + i) % tiku.length
				if(!useTikuList[tmpIndex]){
					useTikuList[tmpIndex] = true
					break
				}
			}
		}
		console.log("tmpIndex : "+tmpIndex)
		cur_index = tmpIndex
		var topic = tiku[cur_index]
		cur_answer = topic.answer
		cur_topic = {
			title : topic.title,
			A : topic.A,
			B : topic.B,
			C : topic.C,
			D : topic.D
		}
		var notify = {
			cmd : "new_topic",
			cur_topic_count : cur_topic_count,
			cur_topic : cur_topic
		}
		room.sendAll(notify)
		countDown = (new Date()).getTime() + answerTime
		topicTimer = setTimeout(local.submit,answerTime)
	}
	//提交答案
	local.submit = function(answer) {
		clearTimeout(topicTimer)
		if(answer && answer === cur_answer){
			//成功
			if(cur_topic_count == max_topic_count){
				console.log("已通关!")
				local.success()
				return
			}else{
				local.chooseTopic()
			}
		}else{
			//失败
			console.log("答题失败!等待复活")
			local.dying()
		}
	}
	//等待复活阶段
	local.dying = function(){
		state = 2
		curOverTime = (new Date()).getTime() + overTime
		overTimer = setTimeout(local.lose,overTime)
		var notify = {
			cmd : "dying",
			curOverTime : curOverTime,
			curTime : (new Date()).getTime()
		}
		room.sendAll(notify)
	}
	//复活
	local.resurgence = function(){
		clearTimeout(overTimer)
		var notify = {
			cmd : "resurgence"
		}
		room.sendAll(notify)
		state = 1
		cur_topic_count--
		local.chooseTopic()
	}
	//成功通关
	local.success = function() {
		GameRemote.dbService.db.hincrby("game:answer:playCount",room.players[0].uid,1)
		GameRemote.dbService.db.hincrby("game:answer:success",room.players[0].uid,1)
		GameRemote.dbService.db.incrby("game:answer:allSuccessNumber",1)
		var notify = {
			cmd : "success"
		}
		room.sendAll(notify)
		local.gameOver()
	}
	//失败
	local.lose = function() {
		GameRemote.dbService.db.hincrby("game:answer:playCount",room.players[0].uid,1)
		var notify = {
			cmd : "lose"
		}
		room.sendAll(notify)
		local.gameOver()
	}
	//游戏结束
	local.gameOver = function() {
		//清除定时器
		clearTimeout(topicTimer)
		clearTimeout(overTimer)
		room.gameOver()
	}
	//开始游戏
	room.handle.gameBegin = function(uid,sid,param,cb) {
		if(state === 0){
			local.gameBegin()
			cb(true)
		}else{
			cb(false)
		}
	}
	//获取当前房间信息
	room.handle.getRoomInfo = function(uid,sid,param,cb) {
		var info = {
			roomType : room.roomType,
			roomId : room.roomId,
			state : state,
			max_topic_count : max_topic_count,
			cur_topic_count : cur_topic_count,
			cur_topic : cur_topic,
			countDown : countDown,
			curTime : (new Date()).getTime(),
			answerTime : answerTime,
			curOverTime : curOverTime,
			overTime : overTime
		}
		cb(true,info)
	}
	//提交答案
	room.handle.submit = function(uid,sid,param,cb) {
		if(state === 1){
			local.submit(param.answer)
			cb(true)
		}else{
			cb(false)
		}
	}
	//放弃游戏
	room.handle.giveUp = function(uid,sid,param,cb) {
		local.lose()
		cb(true)
	}
	//复活
	room.handle.resurgence = function(uid,sid,param,cb) {
		if(lock){
			console.log("lock is true : "+lock)
			cb(false)
			return
		}
		lock = true
		GameRemote.dbService.db.hget("game:answer:resurgenceCount",room.players[0].uid,function(err,data) {
			lock = false
			if(!err && data && parseInt(data) > 0){
				GameRemote.dbService.db.hincrby("game:answer:resurgenceCount",room.players[0].uid,-1)
				local.resurgence()
				cb(true)
			}else{
				cb(false)
			}
		})
	}
	//增加复活次数
	room.handle.addLife = function(uid,sid,param,cb) {
		GameRemote.dbService.db.hincrby("game:answer:resurgenceCount",room.players[0].uid,1)
		cb(true)
	}
	//获取当前复活次数
	room.handle.getLife = function(uid,sid,param,cb) {
		GameRemote.dbService.db.hget("game:answer:resurgenceCount",room.players[0].uid,function(err,data) {
			if(err || !data){
				cb(true,0)
			}else{
				cb(true,data)
			}
		})
	}
	// setTimeout(function() {
	// 	room.gameOver.apply(room)
	// },3000)
	return room
}